$(document).ready(function(){
	
	
	$(".revert_revert").each(function(){
			$(this).bind("click",function(){return revert(this);});
	});

	
	$(".delrevert").each(function(){
	
		$(this).bind("click",function(){
			return delrevert(this);
		});
	});
	
	$(".edittopic").click(function(){
		if($("#revertcontent").attr("id") == "revertcontent"){
			var content = $("#revertcontent").val();
			$("#revertcontent").parent().parent().html(content);
			$("#revertcontent").parent().remove();
		}
		var title = $(".topictitle").text();
		var content = $(this).parent().next().html();
		var	 html =	'<div><p><input id="topictitle" name="topictitle" value="'+ title +'" size="50"/></p>';
		html += '<p><textarea id="topiccontent" name="topiccontent" cols="30" rows="6">'+ content +'</textarea></p>';
		html += '<p><input type="button" class="editpost" value="完成编辑"/>';
		html += ' <input type="button" class="editcancel" value="取消编辑"/>';
		html += '</p</div>';
		$(this).parent().next().html(html);
		var oFCKeditor = new FCKeditor( "topiccontent","90%","300px","Basic") ; 
		oFCKeditor.BasePath = ROOT+"/Public/Js/FCKeditor/" ; 
		oFCKeditor.ReplaceTextarea() ;
		$('.editpost').click(function() {
				postEdit(this);
		});
		$('.editcancel').click(function() {
				postCancel(this,content);
		});
		return false;
	});

	$(".editrevert").each(function(){
		$(this).bind("click",function(){
			return editRevert(this);
		});
		
	});

	

	$("#toptopic").click(function(){
		topTopic(this);
		return false;
	});

	$("#deltopic").click(function(){
		delTopic(this);
		return false;
	});
	

});

function editRevert(obj){
	if($("#topiccontent").attr("id") == "topiccontent"){
		var content = $("#topiccontent").val();
		$("#topiccontent").parent().parent().html(content);
		$("#topiccontent").parent().remove();
	}
	if($("#revertcontent").attr("id") == "revertcontent"){
		var content = $("#revertcontent").val();
		$("#revertcontent").parent().parent().html(content);
		$("#revertcontent").parent().remove();
	}
	var content = $(obj).parent().next().html();
	var	 html =	'<div><p><textarea id="revertcontent" name="revertcontent" cols="30" rows="6">'+ content +'</textarea></p>';
	html += '<p><input type="button" class="editpost" value="完成编辑"/>';
	html += ' <input type="button" class="editcancel" value="取消编辑"/>';
	html += '<input type="hidden" id="revertid" value="'+$(obj).attr("id")+'"/>';
	html += '</p</div>';
	$(obj).parent().next().html(html);
	var oFCKeditor = new FCKeditor( "revertcontent","90%","300px","Basic") ; 
	oFCKeditor.BasePath = ROOT+"/Public/Js/FCKeditor/" ; 
	oFCKeditor.ReplaceTextarea() ;
	$('.editpost').click(function() {
			postRevertEdit(this);
	});
	$('.editcancel').click(function() {
			postCancel(this,content);
	});
	//return false;
}

function postEdit(obj){
	var id = $("#topicid").val();
	var content = getContents("topiccontent")
	if($.trim(content) == ""){
		return false;
	}else{
		$(obj).val("发送数据中...");
		$(obj).attr("disabled", "disabled");
		$(obj).next().attr("disabled", "disabled");
		$.post(APP+"/group/updatetopic", {ajax:1 , id: $("#topicid").val(), title: $("#topictitle").val(), content: content },
			function(data){ 
				if("success" == data){
					$(".topictitle").html($("#topictitle").val());
					$(obj).parent().parent().after(content);
					$(obj).parent().parent().remove();
				}
				//alert(data);
			}  
		);
	}
}

function postRevertEdit(obj){
	var content = getContents("revertcontent")
	if($.trim(content) == ""){
		return false;
	}else{
		$(obj).val("发送数据中...");
		$(obj).attr("disabled", "disabled");
		$(obj).next().attr("disabled", "disabled");
		$.post(APP+"/group/updaterevert", {rid: $("#revertid").val(), ajax: 1, content: content },function(data){ show(obj,data,content); }  );
	}
}

function show(obj,data,content){
	if(data == "编辑成功"){
		$(obj).parent().parent().after(content);
		$(obj).parent().parent().remove();
	}else{
	}
}

function postCancel(obj,content){
	alertMessage(obj,"确定取消编辑？");
	$(".msgOk").bind("click",function(){
		$(obj).parent().parent().after(content);
		$(obj).parent().parent().remove();
		$("#messageBox").remove();
	});
	return false;
	
}

function revert(obj){
	if($(obj).attr("id") == 0){
		var content = "<strong>回复 楼主 "+ $(obj).prev().html() +" </strong>";
		setContents("content","回复 楼主 "+ $(obj).prev().html());
	}else{
		var content = "<strong>回复 "+ $(obj).next().html() + " " + $(obj).prev().html()+" </strong>";
		setContents("content","回复 "+ $(obj).next().html() + " " + $(obj).prev().html());
		$("#revertid").val($(obj).attr("id"));
	}
	$("#reuserid").val($(obj).attr("reuser"));
	content += $(obj).parent().next().html();
	$("#revertfrom").css("display","block");
	$("#revertfrom").html(content);
	
}

function delrevert(obj){
	alertMessage(obj,"确定删除此回复");
	$(".msgOk").bind("click",function(){delRevertAction(obj);});
	return false;
}

function delRevertAction(obj){
	$.get($(obj).attr("href"), {ajax: 1},
		function(data){ 
			if("success" == data){
				$(obj).parent().parent().parent().parent().slideToggle("slow",function(){ $(this).remove(); }); 		
			}
			$("#messageBox").remove(); 
		} 
	);
}

function topTopic(obj){
	if("top" == $(obj).attr("title")){
		alertMessage(obj,"确定要置顶本主题？");
	}else if("untop" == $(obj).attr("title")){
		alertMessage(obj,"确定要取消置顶本主题？");
		
	}
	$(".msgOk").bind("click",function(){topTopicAction(obj);});
	return false;
}
function topTopicAction(obj){
	var url = $(obj).attr("href");
	if("top" == $(obj).attr("title")){
		var url2 = url.replace("toptopic","untoptopic");
		var text = "取消置顶";
		var title = "untop";
	}else{
		var url2 = url.replace("untoptopic","toptopic");
		var text = "置顶本主题";
		var title = "top";
	}
	$.get(url, {ajax: 1},function(data){ 
		if("success" == data){
			$(obj).attr("href",url2);
			$(obj).attr("title",title);	
			$(obj).text(text);
			$("#messageBox").remove();
			msg(obj,"操作成功");
		}else{
			msg(obj,"操作失败");
		}
	});

}
function delTopic(obj){
	alertMessage(obj,"确定要删除本主题？<br/>将导致所有的回复也删除！");
	$(".msgOk").bind("click",function(){delTopicAction(obj);});
	return false;
}
function delTopicAction(obj){
	var url = $(obj).attr("href");
	location.href = url;
}